home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / capturing / digitizershell / macapplication.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  10.6 KB  |  435 lines

  1. /*
  2.     File:        MacApplication.c
  3.  
  4.     Contains:    Digitizer Shell specific functions concerning the application shell.
  5.  
  6.     Written by:     
  7.  
  8.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 7/28/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  20.                 
  21.  
  22. */
  23.  
  24. // INCLUDES
  25. #include "MacApplication.h"
  26. #include "MacFramework.h"
  27. #include "AppConfiguration.h"
  28. #include "DTSQTUtilities.h"
  29.  
  30. // Header file for the specific test functions.
  31. #include "TestFunctions.h"
  32.  
  33. // GLOBALS
  34. long gMaxMilliSecToUse = 0L;        
  35.  
  36. // These are our global SG settings, if needed we need to export these using get/set functions
  37. // as these are internal to this file (.c).
  38. SeqGrabComponent     gSG = NULL;
  39. WindowPtr                gCaptureWindow = NULL;
  40. SGChannel                    gVideoChannel = NULL;
  41. SGChannel                    gAudioChannel = NULL;
  42. SGGrabCompleteBottleUPP    gSGGrabCompleteUPP = 0;
  43.  
  44. static long                    gGrabCompleteCounter = 0L;
  45. static long                    gGrabCompleteTimer = 0L;
  46.  
  47.  
  48. // FUNCTIONS
  49. // ______________________________________________________________________
  50. void DoIdle(WindowRef theWindow)
  51. {
  52.     GrafPtr                 aSavedPort;
  53.     
  54.     GetPort(&aSavedPort);
  55.     SetPort(theWindow);
  56.     
  57.     // Update the SG port assuming it's in use.
  58.     if(gSG != NULL)
  59.          SGIdle(gSG);
  60.     
  61. // @@@INSERT ANY IDLE BASED FUNCTIONALITY HERE
  62.  
  63.     SetPort(aSavedPort);
  64. }
  65.  
  66.  
  67. // ______________________________________________________________________
  68. void DoUpdateWindow(WindowRef theWindow, Rect *theRefreshArea)
  69. {
  70.     #pragma unused(theRefreshArea)
  71.     GrafPtr aSavedPort;
  72.     
  73.     GetPort(&aSavedPort);
  74.     SetPort(theWindow);
  75.     
  76.     BeginUpdate(theWindow);
  77.  
  78.     if(gSG != NULL)
  79.         SGUpdate(gSG, NULL);
  80.         
  81. // @@@INSERT WINDOW SPECIFIC DRAWING FUNCTIONALITY HERE
  82.  
  83.     EndUpdate(theWindow);
  84.     SetPort(aSavedPort);
  85. }
  86.  
  87.  
  88.  
  89. // ______________________________________________________________________
  90. void DoCloseWindow(WindowRef theWindow)
  91. {
  92.     #pragma unused(theWindow)
  93.     OSErr anErr = noErr;
  94.     long nTicks = 0L;
  95.     long finalTicks = TickCount(); 
  96.     float  result;
  97.  
  98.     // Clean up the channels (CloseComponent will handle that).
  99.     anErr = CloseComponent(gSG);  DebugAssert(anErr != noErr);
  100.     gSG = NULL; gVideoChannel = NULL; gAudioChannel = NULL;
  101.     gCaptureWindow = NULL;
  102.  
  103.     // Show any statistics generated while we digitized (weird code but I had problems with float conversions)
  104.     nTicks = finalTicks - gGrabCompleteTimer;            // 1/60:th of a second units
  105.     result = (float) gGrabCompleteCounter/nTicks ;
  106.     printf("The GrabFrameComplete callback was called %7.2f times a second.\n", result * 60.0 );
  107. }
  108.  
  109.  
  110. // ______________________________________________________________________
  111. void     DoDragWindow(WindowRef theWindow, EventRecord *theEvent)
  112. {
  113.     GrafPtr aSavedPort;
  114.     ICMAlignmentProcRecord alignProc;
  115.     
  116.     GetPort(&aSavedPort);
  117.     SetPort(theWindow);
  118.     
  119.     if(gSG != NULL)
  120.     {
  121.         SGPause(gSG, true);
  122.         SGGetAlignmentProc(gSG, &alignProc);
  123.         
  124.         DragAlignedWindow(theWindow, theEvent->where, &qd.screenBits.bounds, NULL, &alignProc);
  125.         SGPause(gSG, false);
  126.     }
  127.     // @@@INSERT WINDOW DRAGGING SPECIFIC FUNCTIONALITY HERE
  128.         
  129.     SetPort(aSavedPort);
  130. }
  131.  
  132. // ______________________________________________________________________
  133. void HandleContentClick(WindowRef theWindow, EventRecord *theEvent)
  134. {
  135.     #pragma unused(theEvent)
  136.     GrafPtr aSavedPort;
  137.     
  138.     GetPort(&aSavedPort);
  139.     SetPort(theWindow);
  140.  
  141.     // @@@INSERT APPLICATION SPECIFIC CONTENT CLICKING FUNCTIONALITY HERE
  142.  
  143.     SetPort(aSavedPort);
  144. }
  145.  
  146.  
  147.  
  148. // ______________________________________________________________________
  149. WindowRef CreateMovieWindow(Rect *theRect, Str255 theTitle)
  150. {
  151.     WindowPtr    aWindow     = NULL;
  152.     Rect                devRect     = {0, 0, 0, 0};
  153.     
  154.         GetBestDeviceRect(NULL, &devRect);
  155.         
  156.         OffsetRect(theRect, devRect.left + 20, devRect.top + 20);
  157.         aWindow = NewCWindow( NULL, theRect, theTitle, true, noGrowDocProc, (WindowPtr)-1,
  158.                                                 true, 0);
  159.     return aWindow;
  160. }
  161.  
  162. // ______________________________________________________________________
  163. void HandleApplicationMenu(short theMenuID, short theMenuItem)
  164. {
  165.     //  HANDLE ANY ADDITIONAL MENU ENTRIES HERE
  166.     switch(theMenuID)
  167.     {
  168.         // Test menus.
  169.         case mTesting:
  170.             switch(theMenuItem)
  171.             {
  172.                 case iTest1:
  173.                 {
  174.                     ShowVDIGInfo();
  175.                     break;
  176.                 }
  177.                 
  178.                 case iTest2:
  179.                 {
  180.                     SetMyVideoChannelSettings();
  181.                     break;
  182.                 }
  183.                 
  184.                 case iTest3:
  185.                 {
  186.                     SetMyAudioChannelSettings();
  187.                     break;
  188.                 }
  189.                 
  190.                 case iTest5:                // Record to file.
  191.                 {
  192.                     RecordSamplesToFile();
  193.                     break;
  194.                 }
  195.             }
  196.             break;
  197.         
  198.         // Capture Size Menus.
  199.         case mCaptureSize:
  200.         {
  201.             switch(theMenuItem)
  202.             {
  203.                 case iSizeNormal:
  204.                 {
  205.                     if( QTUChangeSGWindowSize( GetDefaultSGInstance(), GetDefaultVideoChannel() , gCaptureWindow, 320L, 240L) != noErr)
  206.                         SysBeep(kDefaultSysBeep);
  207.                     break;
  208.                 }
  209.                 
  210.                 case iSizeSmall:
  211.                 {
  212.                     if ( QTUChangeSGWindowSize( GetDefaultSGInstance(), GetDefaultVideoChannel() , gCaptureWindow,160L, 120L) != noErr)
  213.                             SysBeep(kDefaultSysBeep);
  214.                     break;
  215.                 }
  216.                 
  217.                 case iSizeBig:
  218.                 {
  219.                     if ( QTUChangeSGWindowSize( GetDefaultSGInstance(), GetDefaultVideoChannel() , gCaptureWindow, 640L, 480L) != noErr)
  220.                         SysBeep(kDefaultSysBeep);
  221.                     break;
  222.                 }
  223.             }
  224.         break;
  225.         }
  226.         
  227.         // End testing menus
  228.     }
  229. }
  230.  
  231.  
  232. // ______________________________________________________________________
  233. void    AdjustApplicationMenus(void)
  234. {
  235.     MenuHandle mHandle = NULL;
  236.     
  237.     // For the time being always dim the edit entries (maybe later if needed enable these)
  238.     mHandle = GetMenuHandle(mEdit);
  239.     DisableItem(mHandle, iUndo);
  240.     DisableItem(mHandle, iCut);
  241.     DisableItem(mHandle, iCopy);
  242.     DisableItem(mHandle, iPaste);
  243.     DisableItem(mHandle, iClear);
  244.     DisableItem(mHandle, iSelectAll);
  245.     
  246.     // Test if we have a valid capture window open, if true, then enable the right menus, otherwise
  247.     // disable them.
  248.     if(gCaptureWindow != NULL)
  249.     {
  250.         DisableItem(GetMenuHandle(mFile), iNew);
  251.         EnableItem(GetMenuHandle(mFile), iClose);
  252.     
  253.         mHandle = GetMenuHandle(mTesting);
  254.         EnableItem(mHandle, iTest1);
  255.         EnableItem(mHandle, iTest2);
  256.         EnableItem(mHandle, iTest3);
  257.         EnableItem(mHandle, iTest5);
  258.  
  259.         mHandle = GetMenuHandle(mCaptureSize);
  260.         EnableItem(mHandle, iSizeNormal);
  261.         EnableItem(mHandle, iSizeSmall);
  262.         EnableItem(mHandle, iSizeBig);
  263.     }
  264.     else
  265.     {
  266.         EnableItem(GetMenuHandle(mFile), iNew);
  267.         DisableItem(GetMenuHandle(mFile), iClose);
  268.  
  269.         mHandle = GetMenuHandle(mTesting);
  270.         DisableItem(mHandle, iTest1);
  271.         DisableItem(mHandle, iTest2);
  272.         DisableItem(mHandle, iTest3);
  273.         DisableItem(mHandle, iTest5);
  274.  
  275.         mHandle = GetMenuHandle(mCaptureSize);
  276.         DisableItem(mHandle, iSizeNormal);
  277.         DisableItem(mHandle, iSizeSmall);
  278.         DisableItem(mHandle, iSizeBig);
  279.     }
  280. }
  281.  
  282.  
  283. // ______________________________________________________________________
  284. void CreateSGEnviroment(void)
  285. {
  286.     OSErr                        anErr = noErr;
  287.     WindowPtr                 aWin = NULL;
  288.     Rect                            defRect = {20, 20, 260, 340};
  289.     
  290.     if(gSG != NULL || gCaptureWindow != NULL)    // already in use
  291.         return;
  292.     
  293.     gCaptureWindow = CreateMovieWindow(&defRect, "\pSG Grabber Window");
  294.     DebugAssert(gCaptureWindow != NULL);
  295.     
  296.     gSG = QTUCreateSequenceGrabber(gCaptureWindow); DebugAssert(sg != NULL);
  297.     if(gSG == NULL) 
  298.     {
  299.         printf("ERROR: Problem opening the default Sequence Grabber component.\n");
  300.         goto Closure;
  301.     }
  302.     
  303.     anErr = QTUCreateSGGrabChannels(gSG, &gCaptureWindow->portRect, seqGrabPlayDuringRecord + seqGrabRecord,
  304.                          &gVideoChannel, &gAudioChannel); DebugAssert(anErr == noErr); DebugAssert(anErr != noErr);
  305.     if(anErr != noErr) 
  306.     {
  307.         printf("ERROR: Problems creating the Video and Audio SG Channels: %ld.\n", anErr);    
  308.         goto Closure;
  309.     }            
  310.  
  311. #ifdef BOTTLENECKS    
  312.      anErr = SetupVideoBottleNecks(gVideoChannel, gCaptureWindow,  &tempPort); DebugAssert(anErr == noErr);
  313.      if(anErr != noErr)
  314.      {
  315.          printf("ERROR: Problems installing the new video bottle necks: %ld.\n", anErr);
  316.          goto Closure;
  317.      }
  318.      else
  319.          printf("STATUS: Installed video bottle necks.\n");
  320. #endif // BOTTLENECKS
  321.  
  322.     anErr = SGStartPreview(gSG); DebugAssert(anErr == noErr);
  323.     if(anErr != noErr) 
  324.     {
  325.         printf("ERROR: Problems starting the SG Preview: %ld.\n", anErr);
  326.         goto Closure;
  327.     }
  328.     else
  329.         printf("STATUS: Opened Default SG component, created channels (audio, sound), started Preview.\n");
  330.  
  331. Closure:
  332.     return;
  333. }
  334.  
  335.  
  336.  
  337. // ______________________________________________________________________
  338. SeqGrabComponent    GetDefaultSGInstance(void)
  339. {
  340.     return gSG;
  341. }
  342.  
  343.  
  344. SGChannel GetDefaultVideoChannel(void)
  345. {
  346.     return gVideoChannel;
  347. }
  348.  
  349.  
  350. SGChannel GetDefaultAudioChannel(void)
  351. {
  352.     return gAudioChannel;
  353. }
  354.  
  355.  
  356. pascal ComponentResult SpecialGrabFrameComplete(SGChannel c, short bufferNum,
  357.                                                                                 Boolean *done, long refCon)
  358. {
  359.     ComponentResult anErr = noErr;
  360.  
  361.     anErr = SGGrabFrameComplete(c, bufferNum, done); DebugAssert(anErr == noErr);
  362.     
  363.     if(gGrabCompleteCounter != 0)
  364.         gGrabCompleteCounter++;
  365.     else
  366.     {
  367.         gGrabCompleteTimer = TickCount();
  368.         gGrabCompleteCounter++;
  369.     }
  370.         
  371.     if(*done) {    // Frame is done?
  372.         CGrafPtr            tempPort = (CGrafPtr)refCon;
  373.  
  374. #if DRAWING    
  375.         Rect                    bufferRect;
  376.         GDHandle            saveGD;
  377.         PixMapHandle     savePM;
  378.         CGrafPtr            aSavedPort;
  379.         PixMapHandle    bufferPM;
  380.  
  381.         GetGWorld(&aSavedPort, &saveGD);
  382.         SetGWorld(tempPort, NULL);
  383.         
  384.         anErr = SGGetBufferInfo(c, bufferNum, &bufferPM, &bufferRect, NULL, NULL);
  385.         DebugAssert(anErr == noErr);
  386.         
  387.         if(!anErr) {
  388.             
  389.             savePM = tempPort->portPixMap;
  390.             SetPortPix(bufferPM);
  391.             
  392.             TextMode(srcXor);
  393.             MoveTo(bufferRect.right - 20, bufferRect.bottom - 14);
  394.             DrawString("\pHello");
  395.             TextMode(srcOr);
  396.             SetPortPix(savePM);
  397.         }
  398.         SetGWorld(aSavedPort, saveGD);
  399. #endif // DRAWING
  400.     }
  401.     return anErr;
  402. }
  403.  
  404.  
  405.  
  406. OSErr SetupVideoBottleNecks(SGChannel videoChannel, WindowPtr theWindow, CGrafPtr tempPort)
  407. {
  408.     #pragma unused(theWindow)
  409.     OSErr anErr = noErr;
  410.  
  411.     OpenCPort(tempPort);
  412.     SetRectRgn(tempPort->visRgn, -32000, -32000, 32000, 32000);
  413.     CopyRgn(tempPort->visRgn, tempPort->clipRgn);
  414.     PortChanged((GrafPtr)tempPort);
  415.     
  416.     anErr = SGSetChannelRefCon(videoChannel, (long)tempPort); DebugAssert(anErr == noErr);
  417.     if(!anErr){
  418.         VideoBottles vb;
  419.         
  420.         vb.procCount = 9;
  421.         anErr = SGGetVideoBottlenecks(videoChannel, &vb); DebugAssert(anErr == noErr);
  422.         if(!anErr){
  423.             gSGGrabCompleteUPP = NewSGGrabCompleteBottleProc(SpecialGrabFrameComplete);
  424.             vb.grabCompleteProc = gSGGrabCompleteUPP;
  425.             
  426.             anErr = SGSetVideoBottlenecks(videoChannel, &vb); DebugAssert(anErr == noErr);
  427.         }
  428.         
  429.     gGrabCompleteCounter = 0L;    // Setup the timer counter value.
  430.     }
  431.     return anErr;
  432. }
  433.  
  434.  
  435.